<template>
  <div class="login">
    <div class="box">
      <h1 class="title">欢迎登录</h1>
      <el-form
        ref="ruleFormRef"
        label-position="top"
        size="large"
        :model="ruleForm"
        status-icon
        :hide-required-asterisk="true"
        :rules="rules"
      >
        <el-form-item label="账号" prop="userName">
          <el-input
            v-model="ruleForm.userName"
            :prefix-icon="User"
            type="text"
            autocomplete="off"
            placeholder="请输入账号"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            :prefix-icon="Lock"
            @keyup.enter="submitForm(ruleFormRef)"
            type="password"
            autocomplete="off"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button
            color="#3A57FA"
            type="primary"
            @click="submitForm(ruleFormRef)"
          >
            确定
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="copyright">
      Copyright 2021 - 2023 YiTeng. All Rights Reserved
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import { parse } from "qs";
import type { FormInstance, FormRules } from "element-plus";

import { LoginParamType } from "@/api/auth/index";
import { useUserStore } from "@/store/modules/user";
import router from "@/router";

const userStore = useUserStore();
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<LoginParamType>({
  userName: "admin",
  password: "",
});

const rules = reactive<FormRules<typeof ruleForm>>({
  userName: [{ required: true, message: "账号不能为空", trigger: "change" }],
  password: [{ required: true, message: "密码不能为空", trigger: "change" }],
});

// 登录
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return false;
  formEl.validate(async (valid) => {
    if (!valid) return false;
    await userStore.getToken(ruleForm);
    ElMessage.success("Hello，欢迎回来");
  });
};

watch(
  () => userStore.user?.userAppid,
  (val: any) => {
    if (val) {
      // 用户小程序 userAppid 有值后再进行跳转
      const url = parse(window.location.search.slice(1)) as any;
      const redirect = url.redirect ? url.redirect : "/";
      router.push(redirect);
    }
  }
);
</script>

<style lang="scss" scoped>
.login {
  height: 100vh;
  background: url("@/assets/img/bg.jpg") no-repeat 50% 50%;
  background-size: cover;

  .box {
    position: absolute;
    top: 50%;
    left: 50%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 450px;
    height: 550px;
    margin: -275px 0 0 260px;
    background: #fff;
    border-radius: 10px;
  }

  .title {
    color: #333;
    letter-spacing: 10px;
  }

  .copyright {
    position: absolute;
    bottom: 20px;
    left: 50%;
    width: 450px;
    margin-left: 260px;
    font-size: 12px;
    color: #eee;
    text-align: center;
  }

  :deep(.el-form) {
    width: 80%;
  }

  :deep(.el-button) {
    width: 100%;
    margin-top: 20px;
    font-size: 16px;
    letter-spacing: 10px;
  }

  :deep(.el-input .el-input__icon) {
    color: #666;
  }

  :deep(.el-input__wrapper) {
    background: #f7f7f7 !important;
  }
}

@media (height <= 640px) {
  .copyright {
    color: #333 !important;
  }
}
</style>
